package com.chuangsi.project.admin.controller.admin;


import com.chuangsi.common.utils.Assert;
import com.chuangsi.framework.web.controller.BaseController;
import com.chuangsi.framework.web.domain.ResultObject;
import com.chuangsi.project.admin.dto.admin.product.*;
import com.chuangsi.project.admin.service.AgreementService;
import com.chuangsi.project.admin.service.BrandService;
import com.chuangsi.project.admin.service.ModelService;
import com.chuangsi.project.admin.service.ProductService;
import com.chuangsi.project.admin.domain.Product;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author wangshengming
 * @date 2023/3/23
 */
@Api(tags = "产品管理")
@RestController
@RequestMapping("/adminApi/accountAdmin")
public class ProductController  extends BaseController {

    @Autowired
    private ProductService productService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private ModelService modelService;
    @Autowired
    private AgreementService agreementService;

//    @ApiOperation(value = "产品管理分页",notes = "王圣铭")
//    @GetMapping("/productPage")
//    public TableDataInfo productPage(ReqProductPageDto req){
//        List<ResProductPageDto> page = productService.productPage(req);
//        return getDataTable(page);
//    }


    @ApiOperation(value = "产品管理树",notes = "王圣铭")
    @GetMapping("/productTree")
    public ResultObject<List<ResProductTreeDto>> productTree(ReqProductPageDto req){
        List<ResProductTreeDto> data = productService.productTree(req);
        return ResultObject.ok().setData(data);
    }

    @ApiOperation(value = "新增产品",notes = "王圣铭")
    @PostMapping("/addProduct")
    public ResultObject<Void> addProduct(@RequestBody @Valid ReqProductAddDto req){
        Product product = new Product();
        BeanUtils.copyProperties(req,product);
        return productService.save(product)?ResultObject.ok("新增成功"):ResultObject.error("新增失败");
    }

    @ApiOperation(value = "修改产品",notes = "王圣铭")
    @PostMapping("/updateProduct")
    public ResultObject<Void> updateProduct(@RequestBody @Valid ReqProductUpdateDto req){
        Product product = productService.getById(req.getProductId());
        Assert.notNull(product,"未查询到产品详情");
        BeanUtils.copyProperties(req,product);
        return productService.updateById(product)?ResultObject.ok("修改成功"):ResultObject.error("修改失败");
    }

    @ApiOperation(value = "产品详情",notes = "王圣铭")
    @PostMapping("/productDetail")
    public ResultObject<ResProductDetailDto> productDetail(@RequestBody @Valid ReqProductDetailDto req){
        ResProductDetailDto res = productService.productDetail(req);
        return ResultObject.ok("查询成功").setData(res);
    }

    @ApiOperation(value = "删除产品",notes = "王圣铭")
    @PostMapping("/productDel")
    public ResultObject<Void> productDel(@RequestBody @Valid ReqProductDetailDto req){
        productService.removeById(req.getProductId());
        return ResultObject.ok("删除成功");
    }

    @ApiOperation(value = "产品下拉选",notes = "杨文博")
    @PostMapping("/productSelect")
    public ResultObject<List<ReqProductSelectDto>> productSelect(){
        List<ReqProductSelectDto> list = productService.productSelect();
        return ResultObject.ok("查询成功").setData(list);
    }

    @ApiOperation(value = "改变产品状态",notes = "杨文博")
    @PostMapping("/productChangeStatus")
    public ResultObject<Void> productChangeStatus(@RequestBody @Validated ReqProductIdDto dto){
        productService.productChangeStatus(dto.getProductId());
        return ResultObject.ok("变更成功");
    }

    @ApiOperation(value = "改变产品E签宝状态",notes = "杨文博")
    @PostMapping("/productChangeStatusEsgin")
    public ResultObject<Void> productChangeStatusEsgin(@RequestBody @Validated ReqProductIdDto dto){
        productService.productChangeStatusEsgin(dto.getProductId());
        return ResultObject.ok("变更成功");
    }


}
